setwd("~/lab2r")                                                               #set the working directory

library(foreach)                                                                                #load libraries to perform loops in parallel (accelerate code)
library(doMC)
number.of.processors <- 4                                                                       #set the number of available processors (6 is better)
registerDoMC(number.of.processors)

print("Loading wrist...")
dataset.wrist <- read.table("wristClipped.txt")                             #load wrist dataset                             #----- complete -----
print("Loading leg...")
dataset.leg <- read.table("legClipped.txt")                             #load leg dataset                               #----- complete -----
print("Loading torso...")
dataset.torso <- read.table("torsoClipped.txt")                             #load torso dataset                             #----- complete -----
print("Loading labels...")
dataset.labels <- read.table("labelsClipped.txt")                             #load labels dataset                            #----- complete -----

n.samples <- min(c(nrow(dataset.wrist), nrow(dataset.leg), nrow(dataset.torso), nrow(labels)))  #find the number of observations in the datasets (must be the same)

compute.mean.sd <- function(i, data) {
    m <- mean(data[i[1]:i[2]])
    s <- sd(data[i[1]:i[2]])
    c(m, s)
}

for (w.l in seq(from=20, to=100, by=20)) {                                                                                                              #----- complete -----
    print(paste("Computing window length:", w.l))
    overlap <- w.l / 2                                                                          #compute the number of samples to overlap (50%)
    starts <- seq(1, n.samples-w.l+1, by=overlap)                                               #compute the indexes of the start of the windows
    ends <- seq(w.l, n.samples, by=overlap)                                                     #compute the indexes of the end of the windows

                                                                                                #compute the features
                                                                                                #the following table shows the position of the features
    features.wrist <- foreach (dimension = 1:6) %dopar% {                                       #----------------------------------------¬
        t(apply(cbind(starts, ends), 1, compute.mean.sd, data=dataset.wrist[,dimension]))       # average | standard deviation  | sensor |
    }                                                                                           #----------------------------------------|
    features.leg <- foreach (dimension = 1:6) %dopar% {                                         #   1     |      2              | accelX |
        t(apply(cbind(starts, ends), 1, compute.mean.sd, data=dataset.leg[,dimension]))         #   3     |      4              | accelY |      #----- complete -----
    }                                                                                           #   5     |      6              | accelZ |
    features.torso <- foreach (dimension = 1:6) %dopar% {                                       #   7     |      8              | gyroX  |
        t(apply(cbind(starts, ends), 1, compute.mean.sd, data=dataset.torso[,dimension]))      #   9     |      10             | gyroY  |      #----- complete -----
    }                                                                                           #   11    |      12             | gyroZ  |
                                                                                                #-----------------------------------------
    
    gesture.mode <- apply(cbind(starts, ends), 1, function(x){as.integer(names(sort(table(dataset.labels[x[1]:x[2],1]), decreasing=TRUE))[1])}) #----- complete -----

    features.wrist <- do.call(cbind, features.wrist)                                            #transform the results form list to matrix
    features.leg <- do.call(cbind, features.leg)                                                #transform the results form list to matrix
    features.torso <- do.call(cbind, features.torso)                                            #transform the results form list to matrix
    features.all <- cbind(features.wrist, features.leg, features.torso)                       #join the 3 datasets in a single matrix         #----- complete -----

    file.name <- paste("meanSD_w", w.l, "_o", overlap, ".tab", sep="")                          #the name of the files indicates the window size
    write.table(x=features.all, file=file.name, sep="\t", row.names=FALSE, col.names=FALSE)     #write the features in a file
    file.name <- paste("label_w", w.l, "_o", overlap, ".tab", sep="")                           #the name of the files indicates the window size 
    write.table(x=gesture.mode, file=file.name, sep="\t", row.names=FALSE, col.names=FALSE)     #write the labels in a file.
}


